package com.nf.health.app.activity.home.adapter;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.WebViewActivity;
import com.nf.health.app.activity.home.bean.DortorCultrueModel;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.glide.GlideUtil;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.DateUtil;

import java.util.List;


/**
 * 蒙医文化
 */

public class MengYiDoctorCultrueAdapter extends BaseAdapter{

    private Context context;
    private List<DortorCultrueModel> list;
    public MengYiDoctorCultrueAdapter(Context context, List<DortorCultrueModel> list) {
        super();
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        if(list != null){
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView==null) {
            convertView = View.inflate(context, R.layout.item_cultrue_doc,null);
            holder = new ViewHolder();
            holder.iv_cultrue_img = (ImageView) convertView.findViewById(R.id.iv_cultrue_img);
            holder.tv_cultrue_title = (TextView) convertView.findViewById(R.id.tv_cultrue_title);
            holder.tv_cultrue_content = (TextView) convertView.findViewById(R.id.tv_cultrue_content);
            holder.tv_cultrue_timer = (TextView) convertView.findViewById(R.id.tv_cultrue_timer);
            holder.ll_cultrue_mnotice = (LinearLayout) convertView.findViewById(R.id.ll_cultrue_mnotice);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final DortorCultrueModel dortorCultrueModel = list.get(position);
        GlideUtil.loadNetImgC(context, holder.iv_cultrue_img, dortorCultrueModel.getHimg(), R.drawable.default_image);
        holder.tv_cultrue_title.setText(dortorCultrueModel.getTitle());
        holder.tv_cultrue_content.setText(dortorCultrueModel.getOutline()+"");
        String timer = dortorCultrueModel.getTourl();
        if(!TextUtils.isEmpty(timer)){
            if(timer.contains(" ")){
                String timerArray [] = timer.split(" ");
                holder.tv_cultrue_timer.setText(timerArray[0]);
            }else{
                holder.tv_cultrue_timer.setText(timer);
            }
        }else{
            holder.tv_cultrue_timer.setText("");
        }
        String time = DateUtil.date2String(dortorCultrueModel.getCreatetime(), "yyyy-MM-dd hh:mm:ss");
        holder.tv_cultrue_timer.setText(time);

        holder.ll_cultrue_mnotice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               /* Bundle bundle = new Bundle();
                bundle.putSerializable(ContactImpl.MENGYI_CULTRUE__TAG, dortorCultrueModel);
                ActivityUtils.showActivity(context, QueryMengCultureDetailActivity.class, bundle);*/
                Bundle bundle = new Bundle();
//                WebModel webModel = new WebModel();
//                //先设置 右侧按钮是否可显示， 默认右侧都不显示，如果只加载URL，则需要 设置webModel.setUrl即可
//                    webModel.setTAG_USER_SIGN_RIGHT1(1);
//                    webModel.setShowRightImg1(true);
//                webModel.setShowRightImg2(true);
//                webModel.setTAG_USER_SIGN_RIGHT2(1);//显示分享按钮
//                String url = "";
//                /*if(!TextUtils.isEmpty(dortorCultrueModel.getVimg())){
//                    url = dortorCultrueModel.getVimg();
//                }else if(!TextUtils.isEmpty(dortorCultrueModel.getTourl())){
//                    url = dortorCultrueModel.getTourl();
//                }*/
//                url = dortorCultrueModel.getTourl();
//                webModel.setUrl(url);
//                webModel.setId(dortorCultrueModel.getId()+"");
//                webModel.setObject(dortorCultrueModel.getVtype());
//                bundle.putSerializable(WebModel.WEBFLAG_TAG, webModel);
//                ActivityUtils.showActivity(context, NewWebViewActivity.class, bundle);
                bundle.putString(WebViewActivity.KEY_TITLE,dortorCultrueModel.getTitle());
                bundle.putString(WebViewActivity.KEY_IMAGE, dortorCultrueModel.getHimg());
                bundle.putString(WebViewActivity.KEY_URL,dortorCultrueModel.getTourl());
                bundle.putBoolean(WebViewActivity.KEY_IS_SHARE, true);
                bundle.putString(WebViewActivity.KEY_SHARE_CONTENT, dortorCultrueModel.getOutline());
                ActivityUtils.showActivity(context, WebViewActivity.class, bundle);
            }
        });
        return convertView;
    }
    private OnMyClickListener listener;
    public void setOnMyClickListener(OnMyClickListener listener){
        this.listener = listener;
    }

    public void setMynotifyDataChanged(List<DortorCultrueModel> list) {
        this.list =list;
        notifyDataSetChanged();
    }


    public class ViewHolder{
        ImageView iv_cultrue_img;
        TextView tv_cultrue_title;
        TextView tv_cultrue_content;
        TextView tv_cultrue_timer;
        LinearLayout ll_cultrue_mnotice;
    }
}
